package mythread.a09threadsage1;

public class MyThread extends Thread {
    //表示这个类所有的对象，都共享ticket数据
    static int ticket = 0;

    //锁对象：一定是要唯一的
//    static Object obj = new Object();

    @Override
    public void run() {
        synchronized (MyThread.class) {
            while (true) {
                if (ticket < 100) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    ticket++;
                    System.out.println(getName() + "卖出了第" + ticket + "张票");
                }
                else {
                    break;
                }
            }
        }
    }
}